using UnityEngine;
using UnityEngine.UI;

public class GamePanel : BasePanel
{
    [Header("基础数值")]
    public Text hpText;
    public Text damageText;
    public Text armorText;
    public Text coinText;
    public Text yellowKeyText;
    public Text redKeyText;
    public Text blueKeyText;
    public Text flowerText;
    public Text ballText;
    public Text finalSwordText;
    [Header("主名称")]
    public Text mainNameText;
    [Header("道具按钮")]
    public Button compassButton;
    [Header("按钮")]
    public Button enemyButton;
    public Button achiButton;
    public Button redTurnButton;
    public Button keyButton;
    public Button circleButton;
    public Button stoneButton;
    public Button flowerButton;
    public Button ballButton;
    public Button finalSwordButton;
    public Button setButton;  
    public Button eventButton;
    public Image eventIcon;
    [Header("数值")]
    public Text redTurnText;
    public Text keyText;
    public Text circleText;
    public Text stoneText;
    public override void OnClose()
    {
        base.OnClose();

        //关闭消息委托
        EventManager.ReMsgDic("MsgMathData", UpdateMathData);
        EventManager.ReMsgDic("MsgMainName", UpdateMainName);
        EventManager.ReMsgDic("MsgMainBoundle", UpdateBoundleData);
        EventManager.ReMsgDic("MsgMainEventIcon", EventIcon);
    }

    public override void OnInit()
    {
        base.OnInit();
        path="UI/Panel/GamePanel";
        type=PanelType.Panel;

        //消息委托
        EventManager.AddMsgDic("MsgMathData",UpdateMathData);
        EventManager.AddMsgDic("MsgMainName", UpdateMainName);
        EventManager.AddMsgDic("MsgMainBoundle", UpdateBoundleData);
        EventManager.AddMsgDic("MsgMainEventIcon", EventIcon);

        if(GameManager.Instance.haveSword)
        {
            AudManager.instance.PlayBgm(AudManager.instance.bgmDic["拿到终剑"]);
        }
        else
        {
            AudManager.instance.PlayBgm(AudManager.instance.bgmDic["游戏"]);
        }

    }

    public override void OnShow(params object[] par)
    {
        base.OnShow(par);
        //找到UI
        SeekUI();
        //进行UI赋值
        AssignUI(par);
    }
    //找到UI
    public void SeekUI()
    {
        //找到数值
        hpText=item.transform.Find("NumText/HpText").GetComponent<Text>();
        damageText=item.transform.Find("NumText/DamageText").GetComponent<Text>();
        armorText=item.transform.Find("NumText/ArmorText").GetComponent<Text>();
        coinText=item.transform.Find("NumText/CoinText").GetComponent<Text>();
        yellowKeyText=item.transform.Find("NumText/YellowKey").GetComponent<Text>();
        redKeyText=item.transform.Find("NumText/RedKey").GetComponent<Text>();
        blueKeyText=item.transform.Find("NumText/BlueKey").GetComponent<Text>();

        //按钮
        compassButton=item.transform.Find("ItemBar/Viewport/Content/Compass").GetComponent<Button>();
        enemyButton=item.transform.Find("ItemBar/Viewport/Content/Enemy").GetComponent<Button>();
        achiButton=item.transform.Find("ItemBar/Viewport/Content/Achi").GetComponent<Button>();
        redTurnButton=item.transform.Find("ItemBar/Viewport/Content/RedTurn").GetComponent<Button>();
        keyButton=item.transform.Find("ItemBar/Viewport/Content/Key").GetComponent<Button>();
        circleButton=item.transform.Find("ItemBar/Viewport/Content/Circle").GetComponent<Button>();
        stoneButton=item.transform.Find("ItemBar/Viewport/Content/Stone").GetComponent<Button>();
        flowerButton=item.transform.Find("ItemBar/Viewport/Content/Flower").GetComponent<Button>();
        ballButton=item.transform.Find("ItemBar/Viewport/Content/Ball").GetComponent<Button>();
        finalSwordButton = item.transform.Find("ItemBar/Viewport/Content/Sword").GetComponent<Button>();
        eventButton =item.transform.Find("Event").GetComponent<Button>();
        setButton=item.transform.Find("Setting").GetComponent<Button>();
        eventIcon=item.transform.Find("Event").GetComponent<Image>();

        //数值
        redTurnText=item.transform.Find("ItemBar/Viewport/Content/RedTurn/Num").GetComponent<Text>();
        circleText=item.transform.Find("ItemBar/Viewport/Content/Circle/Num").GetComponent<Text>();
        keyText=item.transform.Find("ItemBar/Viewport/Content/Key/Num").GetComponent<Text>();
        stoneText=item.transform.Find("ItemBar/Viewport/Content/Stone/Num").GetComponent<Text>();
        flowerText=item.transform.Find("ItemBar/Viewport/Content/Flower/Num").GetComponent<Text>();
        ballText=item.transform.Find("ItemBar/Viewport/Content/Ball/Num").GetComponent<Text>();
        finalSwordText = item.transform.Find("ItemBar/Viewport/Content/Sword/Num").GetComponent<Text>();
        //赋值
        compassButton.onClick.AddListener(CompassButton);
        enemyButton.onClick.AddListener(EnemyButton);
        achiButton.onClick.AddListener(AchiButton);
        redTurnButton.onClick.AddListener(RedTurnButton);
        keyButton.onClick.AddListener(KeyButton);
        circleButton.onClick.AddListener(CircleButton);
        stoneButton.onClick.AddListener(StoneButton);
        flowerButton.onClick.AddListener(FlowerButton);
        ballButton.onClick.AddListener(BallButton);
        eventButton.onClick.AddListener(EventButton);
        setButton.onClick.AddListener(SetButton);
        finalSwordButton.onClick.AddListener(FinalSwordButton);
        //找到名称
        mainNameText=item.transform.Find("Name/Name").GetComponent<Text>();
        //更新一下框内数据
        GameManager.Instance.UpdateBoundeUI();
    }
    //进行UI赋值
    public void AssignUI(params object[] par)
    {
        GameManager.Instance.UpdateMathData();
    }

    //接受函数
    //赋值数值
    public void UpdateMathData(MsgBase msgBase)
    {
        MsgMathData msg = (MsgMathData)msgBase;

        hpText.text=msg.hp.ToString();
        damageText.text=msg.damage.ToString();
        armorText.text=msg.armor.ToString();
        coinText.text=msg.coin.ToString();
        yellowKeyText.text=msg.yellowKey.ToString();
        redKeyText.text=msg.redKey.ToString();
        blueKeyText.text=msg.blueKey.ToString();
    }
    //更新框
    public void UpdateBoundleData(MsgBase msgBase)
    {
        MsgMainBoundle msg = (MsgMainBoundle)msgBase;

        redTurnText.text=msg.redTurnNum.ToString();
        keyText.text=msg.keyNum.ToString();
        circleText.text=msg.circleNum.ToString();
        stoneText.text=msg.stoneNum.ToString();
        flowerText.text=msg.flowerNum.ToString();
        ballText.text=msg.ballNum.ToString();
        finalSwordText.text = msg.finalSwordNum.ToString();
    }
    //赋值名称
    public void UpdateMainName(MsgBase msgBase)
    {
        MsgMainName msg = (MsgMainName)msgBase;

        mainNameText.text=msg.myName;
    }
    //罗盘按钮
    public void CompassButton()
    {
        //打开选择
        PanelManager.Open<ChooseStairPanel>();

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //敌人按钮
    public void EnemyButton()
    {
        PanelManager.Open<SceneEnemyPanel>();

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //成就按钮
    public void AchiButton()
    {
        PanelManager.Open<AchiAllPanel>();

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //红符按钮
    public void RedTurnButton()
    {
        PanelManager.Open<OtherTipPanel>("红符");

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //钥匙按钮
    public void KeyButton()
    {
        PanelManager.Open<OtherTipPanel>("生锈的钥匙");

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //怨灵柱按钮
    public void CircleButton()
    {
        PanelManager.Open<OtherTipPanel>("怨灵珠");

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //超脱石按钮
    public void StoneButton()
    {
        PanelManager.Open<OtherTipPanel>("超脱石");

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //花按钮
    public void FlowerButton()
    {
        PanelManager.Open<OtherTipPanel>("花");

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //篮球按钮
    public void BallButton()
    {
        PanelManager.Open<OtherTipPanel>("篮球");

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //终剑按钮
    public void FinalSwordButton()
    {
        PanelManager.Open<OtherTipPanel>("终剑");

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //事件按钮
    public void EventButton()
    {
        if(GameManager.Instance.isBloodMoon)
        {
            PanelManager.Open<OtherTipPanel>("血月");
        }
        else
        {
            PanelManager.Open<OtherTipPanel>("太阳");
        }

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //事件图片
    public void EventIcon(MsgBase msg)
    {
        MsgMainEventIcon msgBase = (MsgMainEventIcon)msg;

        eventIcon.sprite=msgBase.sprite;

    }
    //设置按钮
    public void SetButton()
    {
        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
        PanelManager.Open<SettingPanel>();
    }
}
//基础数值
public class MsgMathData:MsgBase
{
    public MsgMathData() { proName="MsgMathData"; }
    //生命，攻击，防御，金币，钥匙
    public int hp;
    public int damage;
    public int armor;
    public float coin;
    public int yellowKey;
    public int redKey;
    public int blueKey;
}
//名称
public class MsgMainName : MsgBase
{
    public MsgMainName() { proName="MsgMainName"; }

    public string myName = "";
}
//框里面的数
public class MsgMainBoundle : MsgBase
{
    public MsgMainBoundle() { proName="MsgMainBoundle"; }

    public int redTurnNum;
    public int keyNum;
    public int stoneNum;
    public int circleNum;
    public int flowerNum;
    public int ballNum;
    public int finalSwordNum;
}
//事件图片
public class MsgMainEventIcon : MsgBase
{
    public MsgMainEventIcon() { proName="MsgMainEventIcon"; }

    public Sprite sprite;
}